let requestUrl = null;

function addHandler() {
	requestUrl = "/equipment_view_baoshi/insert";

	$("#baoshi_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#baoshi_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/equipment_view_baoshi/updateById";

		$("#id").val(rowData.id);
		$("#baoshiId").combobox("setValue", rowData.baoshiId);
		$("#equipmentId").combobox("setValue", rowData.equipmentId);

		$("#baoshi_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "info");
	}
}

$(document).ready(function() {

	$("#equipment_id").combobox({
		url: "/equipment_type/selectTypes",
		valueField: "id",
		textField: "name",
		width: 150,
		prompt: "-装备类型-",
		panelHeight: "auto",
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#baoshi_id").combobox({
		url: "/baoshi_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		prompt: "-宝石类型-",
		panelHeight: "auto",
		formatter: function(row) {
			let text = "";

			text += "<table>";
			text += 	"<tr>"
			text += 		"<td>"
			text += 			"<img width='20' src='" + row.image + "' />";
			text += 		"</td>";
			text += 		"<td>";
			text += 			"<span style='font-size:18px;'>" + row.name + "</span>";
			text += 		"</td>";
			text += 	"</tr>";
			text += "</table>";

			return text;
		}
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		$("#baoshi_list").datagrid("reload", {
			equipmentId: $("#equipment_id").combobox("getValue"),
			baoshiId: $("#baoshi_id").combobox("getValue")
		});
	});
	
	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#equipmentId").combobox({
		url: "/equipment_type/selectTypes",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#baoshiId").combobox({
		url: "/baoshi_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			let text = "";

			text += "<table>";
			text += 	"<tr>"
			text += 		"<td>"
			text += 			"<img width='20' src='" + row.image + "' />";
			text += 		"</td>";
			text += 		"<td>";
			text += 			"<span style='font-size:18px;'>" + row.name + "</span>";
			text += 		"</td>";
			text += 	"</tr>";
			text += "</table>";

			return text;
		}
	});

	$("#baoshi_dialog").dialog({
		title: "装备宝石",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#baoshi_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#baoshi_dialog").dialog("close");
						$("#baoshi_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#baoshi_form").form("clear");
				$("#baoshi_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#baoshi_list").datagrid({
		url: "/equipment_view_baoshi/selectByPage",
		method: "get",
		height: height_table,
		fitColumns: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}],
		columns: [[
			{field: "id", title: "编号", align: "center"},
			{field: "equipmentId", title: "装备类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/equipment_type/selectType", {
						type: value
					}, function(resp) {
						let data = resp.data;

						$("#type__" + rowIndex).attr("title", data.name)
							.attr("src", data.image);
					}, error);
					
					return "<img height='" + size + "' id='type__" + rowIndex + "' />";
				}
			},
			{field: "baoshiId", title: "宝石类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/baoshi_category/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#baoshi_" + rowIndex).attr("title", data.name)
							.attr("src", data.image);
					}, error);

					return "<img height='" + size + "' id='baoshi_" + rowIndex + "' />";
				}
			}
		]]
	});

});